import 'package:bloctest/generated/assets.dart';
import 'package:bloctest/utils/RouteUtils.dart';
import 'package:flutter/material.dart';

import '../../model/knowledge_model.dart';
import '../../utils/Routes.dart';
import '../../utils/base/LifeCyclePage.dart';
import 'knowledge_presenter.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class KnowledgePage extends LifeCyclePage {
  const KnowledgePage({super.key});

  @override
  KnowledgePageState createState() => KnowledgePageState();
}

class KnowledgePageState extends LifeCyclePageState<KnowledgePage> {
  List<KnowledgeItemModel> knowledgeList = [];

  @override
  Widget build(BuildContext context) {
    return SafeArea(child: Container(
        margin: EdgeInsets.only(top: 10.r),
        child: ListView.builder(
          itemBuilder: (context, index) {
            return itemView(context,knowledgeList[index]);
          },
          shrinkWrap: true,
          itemCount: knowledgeList.length,
        )));
  }

  Widget itemView(BuildContext context,KnowledgeItemModel item) {
    return InkWell(onTap:(){
      RouteUtils.pushForName(context, RouterPath.knowledge_detail);

    },child: ConstrainedBox(
      constraints: BoxConstraints(minHeight: 80.h),
      child: Container(
        padding: EdgeInsets.all(10.r),
        margin: EdgeInsets.only(left: 10.r, right: 10.r, bottom: 10.r),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(15.r)),
            border: Border.all(width: 1.r, color: Colors.grey)),
        child: Row(
          children: [
            Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(item.name ?? ''),
                    Text(
                      getItemStr(item),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 2,
                    )
                  ],
                )),
            Image.asset(
              Assets.imagesIconMore,
              width: 20.w,
              height: 20.h,
            )
          ],
        ),
      ),
    ));
  }

  String getItemStr(KnowledgeItemModel item) {
    var list = [];
    item.children?.forEach((element) {
      list.add(element.name);
    });
    return list.isNotEmpty ? list.join(" ") : '';
  }

  @override
  KnowledgePresenter createPresenter() => KnowledgePresenter(this);

  @override
  // TODO: implement presenter
  KnowledgePresenter get presenter => super.presenter as KnowledgePresenter;
}
